package testclient;

import java.io.*;
import java.net.*;

public class BlockingClient implements Runnable {
	
	private int port = 9090;
	private Socket socket ;
	OutputStream os = null;
	PrintWriter pw = null;
	BufferedReader br = null;
	char buf[] = new char[1024];
	String initStr = null;
	
	
	public BlockingClient(String initStr){
		this.initStr = initStr;
	}
	
	public void open() throws IOException {
		this.socket = new Socket("localhost", this.port);
		os = this.socket.getOutputStream();
		pw = new PrintWriter(os);
		br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
	}
	
	public void close() throws IOException {
		this.socket.close();
	}
	
	public void sendString(String data) throws IOException {
		//InputStream is = this.socket.getInputStream();
		//pw.write(data, 0, data.length());
		pw.println(data);
		pw.flush();
		
	}
	
	public void sendFile() throws IOException{
		File file = new File("d:\\bigFile3.txt");
		InputStream is = new FileInputStream(file);
		OutputStream os = this.socket.getOutputStream();
		DataOutputStream dos = new DataOutputStream(os);
		dos.writeLong(file.length());
		
		byte buf[] = new byte[1024];
		int c = 0;
		int idxTry = 0;
		while((c = is.read(buf)) > 0){
			os.write(buf, 0, c);
			System.out.println("Send :" + idxTry++);
		}
		
		is.close();
		os.flush();
		os.close();
	}
	
	public void inputKeyboard() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		
		while((line = br.readLine()) != null){
			if(line.equals("q")){
				br.close();
				this.socket.close();
				break;
			}
			
			this.sendString(line);
			System.out.println("=========>Send Completed..");
			this.readString(line.length());
			System.out.println("=========>Read Completed..");
		}
	}
	
	private void readString(int length) throws IOException {
		String line = null;
		line = br.readLine();
		System.out.println("Server:" + this.initStr + ">" + line);
//		while((line = br.readLine()) != null){
//			System.out.println("Server>" + line);
//		}
				
//		int x= br.read(this.buf, 0, length);
//		System.out.println("length:read = " + length + ":" + x);
//		line = new String(this.buf,0,length);
//		System.out.println("Server>" + line);
	}
	
	@Override
	public void run() {
		
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<1000;i++){
			sb.append(this.initStr);
		}
		System.out.println("Proc:" + this.initStr + " init completed..");
		
		try {
			this.open();
			
			this.sendString(sb.toString() + "\r\n");
			this.readString(0);
			
			this.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	private void sendSampleFileRequest(String path){
		System.out.println("Send Path >" + path );
		DataOutputStream dos = new DataOutputStream(os);
		try {
			dos.writeInt(path.length());
			dos.write(path.getBytes(), 0, path.length());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void main(String ... v){
//		Thread thrA = new Thread(new BlockingClient("a"));
//		Thread thrB = new Thread(new BlockingClient("b"));
//		Thread thrC = new Thread(new BlockingClient("c"));
//		Thread thrD = new Thread(new BlockingClient("d"));
//		Thread thrF = new Thread(new BlockingClient("f"));
//		
//		thrA.start();
//		thrB.start();
//		thrC.start();
//		thrD.start();
//		thrF.start();
		
		BlockingClient client = new BlockingClient("a");
		try {
			client.open();
			client.sendSampleFileRequest("/aaa/bbb/ccc/dd/ff/sample.jpg");
			//client.sendString("HaHa HaHa Ahehe~");
			//client.inputKeyboard();
			//client.sendFile();
			client.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//makeBigFile();
	}
	
	
	public static void makeBigFile(){
		File file = new File("d:\\bigFile3.txt");
		try {
			FileWriter fw = new FileWriter(file);
			for(int i=0;i<10000000;i++){
				fw.write(i+ "\n");
				//if(i%1000000 == 0) System.out.println(">" + i);
			}
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
}
